/**
 * note
 * @author Thot
 */

function TransformableText(defaultTextFormat)
{
	TransformableObject.apply(this);
	
	var field = new tomahawk_ns.InputTextField();
	
	if( defaultTextFormat != undefined )
	{
		field.defaultTextFormat = defaultTextFormat;
	}
	field.padding = 10;
	field.background = false;
	field.border = false;
	this.setBody(field);
	this._field = field;	
	this._field.name = "text"
}

Tomahawk.registerClass( TransformableText, "TransformableText" );
Tomahawk.extend( "TransformableText","TransformableObject" );

TransformableText.ID = 0;
TransformableText.prototype._field = null;
TransformableText.prototype.isText = true;

TransformableText.prototype.setText 		= function(text)
{
	this._field.setText(text);
};

TransformableText.prototype.setTextAlign = function(value)
{
	this._field.setTextAlign(value);
};

TransformableText.prototype.autoFocus = function()
{
	this.setActive(true);
	this._replaceCorners();
	this._activeObject = null;
	this._field.setFocus(true);
};

TransformableText.prototype._mouseHandler 	= function(event)
{	
	if( event.type != tomahawk_ns.MouseEvent.MOUSE_MOVE && 
		event.target != this._field 
	)
	{
		this._field.setFocus(false);
	}
	
	if( 	event.type == tomahawk_ns.MouseEvent.DOUBLE_CLICK 					&& 
			this._field.getFocus() == false 									&& 
			( event.target == this._field || event.target.isChildOf(this._field) ) 
	)
	{
		this._field.setFocus(true);
		this._field._setIndexUnderMouse(event.stageX,event.stageY);
	}
	
	if( this._field.getFocus() == false )
	{
		TransformableObject.prototype._mouseHandler.apply( this, [event] );
	}
};

TransformableText.prototype.unserialize 	= function(obj)
{
	TransformableObject.prototype.unserialize.apply(this,[obj]);
	this._field.setText(obj.text);
	this._field.defaultTextFormat.textColor = obj.defaultTextFormat[0];
	this._field.defaultTextFormat.underline = obj.defaultTextFormat[1];
	this._field.defaultTextFormat.backgroundSelectedColor = obj.defaultTextFormat[2];
	this._field.defaultTextFormat.font = obj.defaultTextFormat[3];
	this._field.defaultTextFormat.bold = obj.defaultTextFormat[4];
	this._field.defaultTextFormat.italic = obj.defaultTextFormat[5];
	this._field.defaultTextFormat.size = obj.defaultTextFormat[6];
	
	var i = obj.formats.length;
	var format = null;
	var formatDesc = null;
	
	while( --i > -1 )
	{
		formatDesc = obj.formats[i];
		format = new tomahawk_ns.TextFormat();
		
		format.textColor = formatDesc[0];
		format.underline = formatDesc[1];
		format.backgroundSelectedColor = formatDesc[2];
		format.font = formatDesc[3];
		format.bold = formatDesc[4];
		format.italic = formatDesc[5];
		format.size = formatDesc[6];
		
		this._field.setTextFormat(format,i,i);
	}
	
	this._field.setTextAlign(obj.align);
};

TransformableText.prototype.serialize 		= function()
{
	var obj = TransformableObject.prototype.serialize.apply(this,[obj]);
	var text = this._field.getText();
	var format = null;
	var i = text.length;
	var formatDesc = null;
	
	obj.type = "TransformableText";
	obj.text = text;
	obj.align = this._field.getTextAlign();
	obj.formats = new Array();
	obj.defaultTextFormat = new Array();
	obj.defaultTextFormat[0] = this._field.defaultTextFormat.textColor;
	obj.defaultTextFormat[1] = this._field.defaultTextFormat.underline;
	obj.defaultTextFormat[2] = this._field.defaultTextFormat.backgroundSelectedColor;
	obj.defaultTextFormat[3] = this._field.defaultTextFormat.font;
	obj.defaultTextFormat[4] = this._field.defaultTextFormat.bold;
	obj.defaultTextFormat[5] = this._field.defaultTextFormat.italic;
	obj.defaultTextFormat[6] = this._field.defaultTextFormat.size;
	
	while( --i > -1 )
	{
		format = this._field.getTextFormat(i);
		formatDesc = new Array();
		formatDesc[0] = format.textColor;
		formatDesc[1] = format.underline;
		formatDesc[2] = format.backgroundSelectedColor;
		formatDesc[3] = format.font;
		formatDesc[4] = format.bold;
		formatDesc[5] = format.italic;
		formatDesc[6] = format.size;
		obj.formats.unshift(formatDesc);
	}
	
	return obj;
};

TransformableText.prototype.resize = function(width,height)
{
	this._body.width = width;
	this._body.height = height;
};

TransformableText.prototype.refreshTextField = function()
{
	this._field._refreshNextFrame = true;
	var i = this._field.children.length;
	var word = null;
	
	while( --i > -1 )
	{
		word = this._field.children[i];
		word.needRefresh = true;
		word.smooth = true;
	}
};

TransformableText.prototype.magnify = function(scale)
{
	var canvas = document.createElement("canvas");
	var context = canvas.getContext("2d");
	var magnifier = 2;
	var texture = new tomahawk_ns.Texture();
	var bmp = null;
	
	this._body.width *= scale * magnifier;
	this._body.height *= scale * magnifier;
	
	this._field.scaleX = scale * magnifier;
	this._field.scaleY = scale * magnifier;
	
	canvas.width = this._body.width;
	canvas.height = this._body.height;
	
	this.draw(context);
	
	this.scaleX = 1 / magnifier;
	this.scaleY = 1 / magnifier;
	
	texture.data = canvas;
	texture.rect = [0,0,canvas.width,canvas.height];
	texture.name = "";
	
	bmp = new tomahawk_ns.Bitmap(texture);
	this.removeChildren();
	this.addChild(bmp);
};



